package com.liu.nb.controller;


import cn.hutool.core.text.replacer.StrReplacer;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.nb.entity.Patroltask;
import com.liu.nb.service.IPatroltaskService;
import com.liu.nb.service.IUserService;
import com.liu.nb.util.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 巡检任务表 前端控制器
 * </p>
 *
 * @author yzl
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/nb/patroltask")
public class PatroltaskController extends BaseController {

    /**
     * 线路管理员新建任务
     *
     * @param principal
     * @param patroltask
     * @return
     */
    @PreAuthorize("hasAnyRole('xianluadmin','root')")
    @PostMapping("/savePatrolTask")
    public Result savePatrolTask(Principal principal, @RequestBody Patroltask patroltask) {
        patroltask.setAdminUid((iUserService.getByUsername(principal.getName())).getId());
        patroltask.setCreartime(LocalDateTime.now());
        patroltask.setCancel(1);
        patroltask.setState(0);

        if (iPatroltaskService.savePatrolTask(patroltask) > 0) {
            return Result.success(true);
        } else {
            return Result.success(false);
        }
    }

    /**
     * 线路管理员分配巡检任务成功后更新巡检任务的状态
     *
     * @param patrolTaskId
     * @return
     */
    @PreAuthorize("hasAnyRole('xianluadmin','root')")
    @PostMapping("/updateState/{patrolTaskId}")
    public Result updateState(@PathVariable String patrolTaskId) {
        Patroltask patroltask = new Patroltask();
        patroltask.setId(patrolTaskId);
        patroltask.setState(1);
        if (iPatroltaskService.updateState(patroltask) > 0) {
            return Result.success(true);
        } else {
            return Result.success(false);
        }

    }

    /**
     * 更改巡检任务
     * @param patroltask
     * @return
     */
    @PreAuthorize("hasAnyRole('xianluadmin','root')")
    @PostMapping("/updateTask/")
    public Result updateTask(@RequestBody Patroltask patroltask){
        boolean b = patroltaskService.updateById(patroltask);
        return Result.success(b);
    }

    /**
     * 线路管理员操作开始巡检任务
     *
     * @param patrolTaskId
     * @param state
     * @return
     */
    @PreAuthorize("hasRole('xianluadmin')")
    @GetMapping("/updateState2")
    public Result updateState2(String patrolTaskId, Integer state) {
        Patroltask patroltask = new Patroltask();

        Integer state2 = null;
        if (state != null && !state.equals("")) {
            state2 = state;
        }

        patroltask.setId(patrolTaskId);
        patroltask.setState(state2);
        return Result.success(iPatroltaskService.updateState(patroltask));
    }

    /**
     * 线路管理员、巡检员根据状态查询相应的巡检任务
     *
     * @param patroltask
     * @param principal
     * @param current
     * @param size
     * @return
     */
    @PreAuthorize("hasAnyRole('xianluadmin','root','xuanjianyuan')")
    @PostMapping("/patrolTaskPageMaps/{current}/{size}")
    public Result patrolTaskPageMaps(@RequestBody Patroltask patroltask, @PathVariable Integer current, @PathVariable Integer size, Principal principal) {
        Integer userId = (iUserService.getByUsername(principal.getName())).getId();
        String name = principal.getName();
        Integer current2 = 1;
        Integer size2 = 5;
        if (current != null && !current.equals("")) {
            current2 = current;
        }
        if (size != null && !size.equals("")) {
            size2 = size;
        }
        IPage<Patroltask> task = patroltaskService.getTask(patroltask, name, userId, current2, size2);
        return Result.success(task);
    }


}
